<?php
//hktun_paypal
include_once(dirname(__FILE__).'/payment/Paypal.php');

class PaypalService {

	var $gateway = "https://www.alipay.com/cooperate/gateway.do?"; //支付接口
	var $parameter; //全部需要传递的参数
	var $security_code; //安全校验码
	var $mysign; //签名
	
	//eric
		//initial paypal class
	var	$myPaypal;
	

	//构造支付宝外部服务接口控制
	function PaypalService($parameter, $security_code, $sign_type = "MD5", $transport = "https") {

		$this->myPaypal = new Paypal();
	
		$this->set_directpay_fields($parameter);
	}

	//eric set fields
	function set_directpay_fields($parameter)
	{
		while (list ($key, $val) = each ($parameter)) {
			//echo "key=.$key    value=.$val";
			
			if($key == "return_url")
				$this->myPaypal->addField('return', $val);
			
				
			if($key == "total_fee")
				$this->myPaypal->addField('amount', $val);
				
			if($key == "seller_email")
				$this->myPaypal->addField('business', $val);
				//hktun_paypal
				//$this->myPaypal->addField('business', 'hktun.china@gmail.com');
				
				
//			echo $this->myPaypal->addField('notify_url', $val);
			if($key == "notify_url")
				$this->myPaypal->addField('notify_url', $val);
				
			if($key == "service")
				$this->myPaypal->addField('item_name', $val);
			if($key == "out_trade_no")
				$this->myPaypal->addField('custom', $val);	
				
			//fix
			$this->myPaypal->addField('currency_code', 'HKD');
			//$this->myPaypal->addField('amount', '1');	
			
//			
		}
	}
	
	//若使用GET方式传递，请使用create_url函数获得完整URL链接
	function create_url() {
		$url = $this->gateway;
		$sort_array = array ();
		$arg = "";
		$sort_array = $this->arg_sort($this->parameter);
		while (list ($key, $val) = each($sort_array)) {
			$arg .= $key . "=" . urlencode($this->charset_encode($val, $this->parameter['_input_charset'], $this->parameter['_input_charset'])) . "&";
		}
		$url .= $arg . "sign=" . $this->mysign . "&sign_type=" . $this->sign_type;
		return $url;
	}

	//若使用POST方式传递，请使用Get_Sign函数获得加密结果字符串
	function Get_Sign() {
		// Enable test mode if needed
		//hktun_paypal_testmode
		//$this->myPaypal->enableTestMode();

		// Let's start the train!
		$this->myPaypal->submitPayment();
	}

	function arg_sort($array) {
		ksort($array);
		reset($array);
		return $array;
	}

	function sign($prestr) {
		$mysign = "";
		if ($this->sign_type == 'MD5') {
			$mysign = md5($prestr);
		}
		elseif ($this->sign_type == 'DSA') {
			//DSA 签名方法待后续开发
			die("DSA 签名方法待后续开发，请先使用MD5签名方式");
		} else {
			die("支付宝暂不支持" . $this->sign_type . "类型的签名方式");
		}
		return $mysign;
	}
	function para_filter($parameter) { //除去数组中的空值和签名模式
		$para = array ();
		while (list ($key, $val) = each($parameter)) {
			if ($key == "sign" || $key == "sign_type" || $val == "")
				continue;
			else
				$para[$key] = $parameter[$key];
		}
		return $para;
	}
	//实现多种字符编码方式
	function charset_encode($input, $_output_charset, $_input_charset) {
		$output = "";
		if (!isset ($_output_charset))
			$_output_charset = $this->parameter['_input_charset'];
		if ($_input_charset == $_output_charset || $input == null) {
			$output = $input;
		}
		elseif (function_exists("mb_convert_encoding")) {
			$output = mb_convert_encoding($input, $_output_charset, $_input_charset);
		}
		elseif (function_exists("iconv")) {
			$output = iconv($_input_charset, $_output_charset, $input);
		} else
			die("sorry, you have no libs support for charset change.");
		return $output;
	}
}
?>
